if (!window.XMLHttpRequest) XMLHttpRequest = function()
{
  try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
  try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
  try{ return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try{ return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
  throw new Error("Could not find an XMLHttpRequest Alternative.")
};

function Request(function_name,opt_argv,action_str) 
{
  if (!opt_argv)
  {
    opt_argv = new Array();
  }

  var callback = null;
  var len = opt_argv.length;

  if (len > 0 && typeof opt_argv[len-1] == 'function')
  {
    callback = opt_argv[len-1];
    opt_argv.length--;
  }
  var async = (callback != null);
  var params = new Array(function_name);

  for (var i = 0; i < opt_argv.length; i++)
  {
    params.push(opt_argv[i]);
  }
  var body = JSON.stringify(params);
  var req = new XMLHttpRequest();
  req.open('POST', action_str, async);

  req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  req.setRequestHeader("Content-length",body.length);
  req.setRequestHeader("Connection","close");


  if (async) 
  {
    req.onreadystatechange = function()
    {
	 if(req.readyState == 4 && req.status== 200)
	 {
      var response = null;
      try
      {
        response = JSON.parse(req.responseText);
      }
      catch(e)
      {
        response = req.responseText;
      }
      callback(response);
	  }
    }
  }

  req.send(body);
}

/**
 * codeAddress uses Google Maps json api to query for
 * latitude and longitude.
 */
function codeAddress()
{
  var geocoder = new google.maps.Geocoder();
  var address = document.getElementById("address").value;
  if (geocoder)
  {
    geocoder.geocode( {'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK)
	  {
	    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
		  var coordinates = document.getElementById("id_coordinates");
		  var location_str = new String(results[0].geometry.location);
		  alert("Trying: " + location_str.match(/(\d+.\d+)/g));
		  coordinates.value = location_str.match(/(\d+.\d+)/g);
		  // alert("Bounds=" + results[0].geometry.bounds);
		} else {
		  alert("No results found");
		}
	  } else {
	    alert("Geocode was not successful for the following reason: " + status);
	  }
	});
  }
}

function MoveOption(source_id,target_id)
{
  var source_element = document.getElementById(source_id);
  var target_element = document.getElementById(target_id);
  for(var i=0;i<source_element.options.length;i++)
  {
    var source_option = source_element.options[i];
	if(source_option.selected)
	{
	  var new_option = new Option();
	  new_option.value = source_option.value;
	  new_option.text = source_option.text;
	  target_element.add(new_option,null);
	}
  }
}
function RemoveOption(select_id)
{
  var select = document.getElementById(select_id);
  for(var i=0;i<select.options.length;i++)
  {
    if(select.options[i].selected)
	{
	  select.remove(i);
	}
  }
}